package com.gy.niuer.mapper;
import org.apache.ibatis.annotations.Param;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.gy.niuer.domain.Department;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
* @Entity com.gy.niuer.domain.Department
*/
@Mapper
public interface DepartmentMapper extends BaseMapper<Department> {

    @Select("select * from department where parent = #{parent}")
    List<Department> selectAllSub(int parent);

    /**
     * 查看父类部门包括子类部门
     * 查询一级部门的id的时候，去调用selectAllsub方法，
     *  * 这个方法是通过一级部门的id找二级部门.
     *  * selectAllSub方法返回一个集合，将这个集合赋值给属性sub
     * @return
     */
    //当字段和属性不一致的时候使用results
    @Results(id = "departmentResults",value={
            //先将字段id的值赋值给属性id
            @Result(column = "id",property = "id"),
            //column：字段   property:属性 将id的值赋值给selectAllSub方法去调用查询他的子部门
            @Result(column = "id",property = "sub",many = @Many(select="selectAllSub"))
    })
    @Select("select * from department where parent is null")  //查询的一级部门
    List<Department> selectAll();

    Department selectOneById(@Param("id") Integer id);
}
